package com.example.starter.work4

import mu.KotlinLogging
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently
import java.text.SimpleDateFormat
import java.util.*


/**
@author whd
@date 2023/10/10 20:16:01
 */
object Consumer {
  private val logger = KotlinLogging.logger {}
  @JvmStatic
  fun main(args: Array<String>) {
    val consumer = DefaultMQPushConsumer("Msg12")
    consumer.namesrvAddr = "192.168.201.129:9876"
    consumer.subscribe("topic", "*")
    consumer.registerMessageListener(MessageListenerConcurrently { msgs, _ ->
      for (msg in msgs) {
        val messageBody = String(msg.body)
        val currentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())
        logger.debug("收到消息时间"+currentTime.toString()+messageBody)
      }
      ConsumeConcurrentlyStatus.CONSUME_SUCCESS
    })
    consumer.start() // 启动消费者
  }
}
